const {
  db
} = require('../core/db.js');

const util = require('util');
const axios = require('axios');
const {
  Sequelize,
  Model,
  DataTypes,
  Op
} = require('sequelize');

const { detailUrl, keywordUrl } = require('../config/config.js').yushu;

class Book extends Model {
  // constructor(id) {
  //   super();
  //   this.id = id;
  // }
  async detail (id) {
    const url = util.format(detailUrl, id);
    const detail = await axios.get(url);
    return detail.data;
  }

  static async searchFromYunShu(q, start, count, summary = 1){
    const url = util.format(keywordUrl, encodeURL(q), count, start);
    const result = await axios.get(url);
    return result.data;
  }
};

Book.init({
  id: {
    type: Sequelize.INTEGER,
    primaryKey: true
  },
  fav_nums: {
    type: Sequelize.INTEGER,
    default: 0
  }
}, {
  sequelize: db,
  tableName: 'book'
});

module.exports = {
  Book
}